<?php
//	update exam score.
header('Content-type: application/json');
include "db.conf.php";
$sTableName="2014_w_la";
$sTeamwork="1";
$sExamName="final";

$sqlConnect = new mysqli($sServer,$sUser,$sPassword,$sDatabaseName);
$sqlConnect->query("SET NAMES 'UTF8'");

/*echo "<?xml version=\"1.0\" encoding=\"utf-8\"?><result>";*/
//Check connection
if ($sqlConnect ->connect_error) {
	$result=array('error'=>"Can't connect to database");
}
else
{
	$sQuery = "SELECT `Name` FROM `".$sTableName."` WHERE `student_number`=".$_REQUEST["StudentID"];
	//echo $sQuery;
	$sqlResult=$sqlConnect->query($sQuery);
	$numrow = $sqlResult->num_rows;
	
	if ($numrow===0)
	{
		$result=array('error'=>"No students found");
	}
	else {if ($numrow===1)
		{	
			$row=$sqlResult->fetch_row();
			$nTotalScore=0;
			$sQuery = "UPDATE `".$sTableName."` SET ";
			for ($nProblem=1;$nProblem<8;$nProblem++)
			{
				$nTotalScore+=$_REQUEST["m".$nProblem];
				$sQuery.=("`".$sExamName."_".$nProblem."` =".$_REQUEST["m".$nProblem].",");
			}
			$sQuery.="`exam_order`=".$_REQUEST["examOrder"].",`".$sExamName."`=".$nTotalScore." WHERE `student_number`=".$_REQUEST["StudentID"];
			//`".$sTeamwork."`=".$_REQUEST["Score"]." WHERE `StudentID`=".$_REQUEST["StudentID"];
			//echo $sQuery;
			$sqlResult = $sqlConnect->query($sQuery);
			$result=array('examOrder'=>$_REQUEST["examOrder"]+1,'StudentID'=>$_REQUEST["StudentID"],'Name'=>$row[0],'score'=>$nTotalScore);
		}
	}
}
/*	*/
$sqlConnect->close();
echo json_encode($result);
?>
